﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Xml;
using AsmResolver;
using AsmResolver.PE;
using AsmResolver.PE.Win32Resources.Builder;
using AsmResolver.PE.Win32Resources.RCData;
using AsmResolver.PE.Win32Resources.String;
using AsmResolver.PE.Win32Resources.Version;
using Borland.Vcl;
using Borland.Vcl.Units;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Convert = System.Convert;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using dnlib.DotNet.Resources;
using NPOI.SS.Util;
using NPOI.OpenXmlFormats.Spreadsheet;
using NPOI.HSSF.UserModel;

namespace Delphi翻译
{
    [System.Security.SecuritySafeCritical]
    public partial class Form1 : Form
    {
        string[] TranslateDotNetFiles;

        string[] TranslateNativePEFiles;

        private void UpdateTargetFiles(string ProductVersion)
        {
            TranslateDotNetFiles = new string[] {
                $@"bin\Borland.Build.Tasks.Common.dll",
            $@"bin\Borland.Build.Tasks.Delphi.dll",
            $@"bin\Borland.Build.Tasks.Shared.dll",
            $@"bin\Borland.Delphi.dll",
            $@"bin\Borland.Studio.Delphi.dll",
            $@"bin\Borland.Studio.Host.dll",
            $@"bin\Borland.Studio.Interop.dll",
            $@"bin\Borland.Studio.Refactoring.dll",
            $@"bin\Borland.Studio.ToolsAPI.dll",
            $@"bin\Borland.Studio.Vcl.Design.Refactoring.dll",
            $@"bin\Borland.VclRtl.dll",
            $@"bin\DelphiProvider.dll",
            $@"bin\Embarcadero.Modeling.Formatter.dll",
        };

            TranslateNativePEFiles = new string[]{
            $@"bin\adortl{ProductVersion}.bpl",
            $@"bin\appanalytics{ProductVersion}.bpl",
            $@"bin\applet{ProductVersion}.bpl",
            $@"bin\asmview{ProductVersion}.bpl",
            $@"bin\AuditsCLI.exe",
            $@"bin\AzureCloud{ProductVersion}.bpl",
            $@"bin\bcbcoment{ProductVersion}.bpl",
            $@"bin\bcbcompro{ProductVersion}.bpl",
            $@"bin\bcbcorba{ProductVersion}.bpl",
            $@"bin\bcbent{ProductVersion}.bpl",
            $@"bin\bcbfmxide{ProductVersion}.bpl",
            $@"bin\bcbfmxmobile{ProductVersion}.bpl",
            $@"bin\bcbide{ProductVersion}.bpl",
            $@"bin\bcbie{ProductVersion}.bpl",
            $@"bin\bcbiec{ProductVersion}.bpl",
            $@"bin\bcboffice2k{ProductVersion}.bpl",
            $@"bin\bcbofficexp{ProductVersion}.bpl",
            $@"bin\bcbpro{ProductVersion}.bpl",
            $@"bin\bcbsmp{ProductVersion}.bpl",
            $@"bin\bcbsmpc{ProductVersion}.bpl",
            $@"bin\bcbvclide{ProductVersion}.bpl",
            $@"bin\bcbwin32{ProductVersion}.bpl",
            $@"bin\bcbwin64{ProductVersion}.bpl",
            $@"bin\bcc32.exe",
            $@"bin\bcc32c.exe",
            $@"bin\bcc32x.exe",
            $@"bin\bcc64.exe",
            $@"bin\bccbrow.dll",
            $@"bin\bccide.dll",
            $@"bin\BCompareLite.exe",
            $@"bin\bds.exe",
            $@"bin\BDSFileCopyHelper.exe",
            $@"bin\BDSLauncher.exe",
            $@"bin\bdsreg.exe",
            $@"bin\BDSSetLang.exe",
            $@"bin\bindcomp{ProductVersion}.bpl",
            $@"bin\bindcompdbx{ProductVersion}.bpl",
            $@"bin\bindcompfmx{ProductVersion}.bpl",
            $@"bin\bindcompvcl{ProductVersion}.bpl",
            $@"bin\bindcompvclsmp{ProductVersion}.bpl",
            $@"bin\bindengine{ProductVersion}.bpl",
            $@"bin\bindgraph{ProductVersion}.bpl",
            $@"bin\BitmapStyleDesigner.exe",
            $@"bin\bordbk{ProductVersion}.dll",
            $@"bin\bordbk{ProductVersion}N.dll",
            $@"bin\boreditu.dll",
            $@"bin\borlndmm.dll",
            $@"bin\brc32.exe",
            $@"bin\brcc32.exe",
            $@"bin\brcide.dll",
            $@"bin\cc32{ProductVersion}.dll",
            $@"bin\cc32{ProductVersion}mt.dll",
            $@"bin\cc32c{ProductVersion}.dll",
            $@"bin\cc32c{ProductVersion}mt.dll",
            $@"bin\cg32.dll",
            $@"bin\cgconfig.exe",
            $@"bin\cghelp.dll",
            $@"bin\cgrc.exe",
            $@"bin\CloudService{ProductVersion}.bpl",
            $@"bin\codeguardide{ProductVersion}.bpl",
            $@"bin\codequerybcb{ProductVersion}.bpl",
            $@"bin\codequerydelphi{ProductVersion}.bpl",
            $@"bin\codequeryide{ProductVersion}.bpl",
            $@"bin\codetemplates{ProductVersion}.bpl",
            $@"bin\coff2omf.exe",
            $@"bin\comcore{ProductVersion}.bpl",
            $@"bin\comentcore{ProductVersion}.bpl",
            $@"bin\comp32x.dll",
            $@"bin\comp32xmod.dll",
            $@"bin\compclang33.dll",
            $@"bin\compclang50.dll",
            $@"bin\CompLLVM.dll",
            $@"bin\compllvm35.dll",
            $@"bin\comptoolbar{ProductVersion}.bpl",
            $@"bin\convert.exe",
            $@"bin\coreide{ProductVersion}.bpl",
            $@"bin\coreproide{ProductVersion}.bpl",
            $@"bin\cp3245mt.dll",
            $@"bin\cpp32.exe",
            $@"bin\cpp32c.exe",
            $@"bin\cpp64.exe",
            $@"bin\CppLibTransform.exe",
            $@"bin\CppStructureParser.dll",
            $@"bin\CustomIPTransport{ProductVersion}.bpl",
            $@"bin\darktheme{ProductVersion}.bpl",
            $@"bin\DataExplorerDBXPlugin{ProductVersion}.bpl",
            $@"bin\DataExplorerDBXPluginEnt{ProductVersion}.bpl",
            $@"bin\DataExplorerDBXPluginInt{ProductVersion}.bpl",
            $@"bin\DataExplorerFireDACPlugin{ProductVersion}.bpl",
            $@"bin\DataExplorerIDE{ProductVersion}.bpl",
            $@"bin\DataExplorerService{ProductVersion}.bpl",
            $@"bin\DataSnapClient{ProductVersion}.bpl",
            $@"bin\DataSnapCommon{ProductVersion}.bpl",
            $@"bin\DataSnapConnectors{ProductVersion}.bpl",
            $@"bin\DataSnapConnectorsFreePascal{ProductVersion}.bpl",
            $@"bin\DataSnapFireDAC{ProductVersion}.bpl",
            $@"bin\DataSnapIndy10ServerTransport{ProductVersion}.bpl",
            $@"bin\DataSnapNativeClient{ProductVersion}.bpl",
            $@"bin\DataSnapProviderClient{ProductVersion}.bpl",
            $@"bin\DataSnapServer{ProductVersion}.bpl",
            $@"bin\DataSnapServerMidas{ProductVersion}.bpl",
            $@"bin\dbexpress{ProductVersion}.bpl",
            $@"bin\dbkdebugide{ProductVersion}.bpl",
            $@"bin\dbkdebugproide{ProductVersion}.bpl",
            $@"bin\dbkw64srv_27_0.dll",
            $@"bin\dbkw64_27_0.dll",
            $@"bin\dbkw64_27_0.exe",
            $@"bin\dbrtl{ProductVersion}.bpl",
            $@"bin\dbtest.bpl",
            $@"bin\dbx{ProductVersion}.bpl",
            $@"bin\dbxadapter.dll",
            $@"bin\dbxasa.dll",
            $@"bin\dbxase.dll",
            $@"bin\dbxcds{ProductVersion}.bpl",
            $@"bin\DBXClientDriver{ProductVersion}.bpl",
            $@"bin\DbxCommonDriver{ProductVersion}.bpl",
            $@"bin\DbxDb2.dll",
            $@"bin\DBXDb2Driver{ProductVersion}.bpl",
            $@"bin\dbxfb.dll",
            $@"bin\DBXFirebirdDriver{ProductVersion}.bpl",
            $@"bin\dbxinf.dll",
            $@"bin\DBXInformixDriver{ProductVersion}.bpl",
            $@"bin\dbxint.dll",
            $@"bin\DBXInterBaseDriver{ProductVersion}.bpl",
            $@"bin\dbxmss.dll",
            $@"bin\dbxmss9.dll",
            $@"bin\DBXMSSQLDriver{ProductVersion}.bpl",
            $@"bin\dbxmys.dll",
            $@"bin\DBXMySqlDriver{ProductVersion}.bpl",
            $@"bin\DBXOdbcDriver{ProductVersion}.bpl",
            $@"bin\dbxora.dll",
            $@"bin\DBXOracleDriver{ProductVersion}.bpl",
            $@"bin\DBXSqliteDriver{ProductVersion}.bpl",
            $@"bin\DBXSybaseASADriver{ProductVersion}.bpl",
            $@"bin\DBXSybaseASEDriver{ProductVersion}.bpl",
            $@"bin\dcc32{ProductVersion}.dll",
            $@"bin\dcc64.exe",
            $@"bin\dcc64{ProductVersion}.dll",
            $@"bin\dcc64{ProductVersion}N.dll",
            $@"bin\dccios32{ProductVersion}.dll",
            $@"bin\dcl31w{ProductVersion}.bpl",
            $@"bin\dclact{ProductVersion}.bpl",
            $@"bin\dclado{ProductVersion}.bpl",
            $@"bin\dclappanalytics{ProductVersion}.bpl",
            $@"bin\dclbcbsmp{ProductVersion}.bpl",
            $@"bin\dclbindcomp{ProductVersion}.bpl",
            $@"bin\dclbindcompdbx{ProductVersion}.bpl",
            $@"bin\dclbindcompfiredac{ProductVersion}.bpl",
            $@"bin\dclbindcompfmx{ProductVersion}.bpl",
            $@"bin\dclbindcompvcl{ProductVersion}.bpl",
            $@"bin\dclCloudService{ProductVersion}.bpl",
            $@"bin\dclcommon{ProductVersion}.bpl",
            $@"bin\dclDataSnapClient{ProductVersion}.bpl",
            $@"bin\dclDataSnapCommon{ProductVersion}.bpl",
            $@"bin\dclDataSnapConnectors{ProductVersion}.bpl",
            $@"bin\dclDataSnapFireDAC{ProductVersion}.bpl",
            $@"bin\dclDataSnapIndy10ServerTransport{ProductVersion}.bpl",
            $@"bin\dclDataSnapNativeClient{ProductVersion}.bpl",
            $@"bin\dclDataSnapNativeServer{ProductVersion}.bpl",
            $@"bin\dclDataSnapProviderClient{ProductVersion}.bpl",
            $@"bin\dclDataSnapServer{ProductVersion}.bpl",
            $@"bin\dclDataSnapWebBrokerServer{ProductVersion}.bpl",
            $@"bin\dcldb{ProductVersion}.bpl",
            $@"bin\dcldbx{ProductVersion}.bpl",
            $@"bin\dcldbxcds{ProductVersion}.bpl",
            $@"bin\dclDBXDrivers{ProductVersion}.bpl",
            $@"bin\dclDBXDriversEnt{ProductVersion}.bpl",
            $@"bin\dclDBXDriversInt{ProductVersion}.bpl",
            $@"bin\dcldsnapxml{ProductVersion}.bpl",
            $@"bin\dcledge{ProductVersion}.bpl",
            $@"bin\dclemacsedit{ProductVersion}.bpl",
            $@"bin\dclemsclient{ProductVersion}.bpl",
            $@"bin\dclemsclientfiredac{ProductVersion}.bpl",
            $@"bin\dclEMSEdge{ProductVersion}.bpl",
            $@"bin\dclemsserver{ProductVersion}.bpl",
            $@"bin\dclExpertsCreators{ProductVersion}.bpl",
            $@"bin\dclExpertsUI{ProductVersion}.bpl",
            $@"bin\dclFireDAC{ProductVersion}.bpl",
            $@"bin\dclFireDACEnt{ProductVersion}.bpl",
            $@"bin\dclFireDACEntExt{ProductVersion}.bpl",
            $@"bin\dclFmxFireDAC{ProductVersion}.bpl",
            $@"bin\dclfmxstd{ProductVersion}.bpl",
            $@"bin\dclie{ProductVersion}.bpl",
            $@"bin\dclIndyCore{ProductVersion}.bpl",
            $@"bin\dclIndyProtocols{ProductVersion}.bpl",
            $@"bin\dclIPIndyImpl{ProductVersion}.bpl",
            $@"bin\dclmcn{ProductVersion}.bpl",
            $@"bin\dclmid{ProductVersion}.bpl",
            $@"bin\dclmlwiz{ProductVersion}.bpl",
            $@"bin\dclnet{ProductVersion}.bpl",
            $@"bin\dclnetdb{ProductVersion}.bpl",
            $@"bin\dclnetwiz{ProductVersion}.bpl",
            $@"bin\dcloffice2010{ProductVersion}.bpl",
            $@"bin\dcloffice2K{ProductVersion}.bpl",
            $@"bin\dclofficexp{ProductVersion}.bpl",
            $@"bin\dclQuickEdit{ProductVersion}.bpl",
            $@"bin\dclRESTBackendComponents{ProductVersion}.bpl",
            $@"bin\dclRESTComponents{ProductVersion}.bpl",
            $@"bin\dclrtl{ProductVersion}.bpl",
            $@"bin\dclshare{ProductVersion}.bpl",
            $@"bin\dclsmp{ProductVersion}.bpl",
            $@"bin\dclsmpedit{ProductVersion}.bpl",
            $@"bin\dclsoap{ProductVersion}.bpl",
            $@"bin\dclsoapmidas{ProductVersion}.bpl",
            $@"bin\dclsoapserver{ProductVersion}.bpl",
            $@"bin\dclsockets{ProductVersion}.bpl",
            $@"bin\dclstd{ProductVersion}.bpl",
            $@"bin\dcltethering{ProductVersion}.bpl",
            $@"bin\dcltouch{ProductVersion}.bpl",
            $@"bin\dclvcldb{ProductVersion}.bpl",
            $@"bin\dclVclFireDAC{ProductVersion}.bpl",
            $@"bin\dclWindowsAzureManagement{ProductVersion}.bpl",
            $@"bin\dclwinx{ProductVersion}.bpl",
            $@"bin\dclxml{ProductVersion}.bpl",
            $@"bin\delphicoment{ProductVersion}.bpl",
            $@"bin\delphicompro{ProductVersion}.bpl",
            $@"bin\delphicoreide{ProductVersion}.bpl",
            $@"bin\delphicoreproide{ProductVersion}.bpl",
            $@"bin\delphide{ProductVersion}.bpl",
            $@"bin\delphierrorinsite{ProductVersion}.bpl",
            $@"bin\delphifmxide{ProductVersion}.bpl",
            $@"bin\delphifmxmobile{ProductVersion}.bpl",
            $@"bin\DelphiLSP.exe",
            $@"bin\delphipro{ProductVersion}.bpl",
            $@"bin\delphivclide{ProductVersion}.bpl",
            $@"bin\delphiwin32{ProductVersion}.bpl",
            $@"bin\delphiwin64{ProductVersion}.bpl",
            $@"bin\designide{ProductVersion}.bpl",
            $@"bin\DeviceManager{ProductVersion}.bpl",
            $@"bin\dfm{ProductVersion}.bpl",
            $@"bin\DotnetCoreAssemblies{ProductVersion}.bpl",
            $@"bin\dotnetcoreide{ProductVersion}.bpl",
            $@"bin\drinterop.exe",
            $@"bin\dsnap{ProductVersion}.bpl",
            $@"bin\dsnapcon{ProductVersion}.bpl",
            $@"bin\dsnapxml{ProductVersion}.bpl",
            $@"bin\DSProxyGen.exe",
            $@"bin\DSProxyReg.exe",
            $@"bin\dsymlink.exe",
            $@"bin\emsclient{ProductVersion}.bpl",
            $@"bin\emsclientfiredac{ProductVersion}.bpl",
            $@"bin\EMSConsole.dll",
            $@"bin\EMSDevConsole.exe",
            $@"bin\EMSDevServer.exe",
            $@"bin\EMSDevServerCommand.exe",
            $@"bin\emsedge{ProductVersion}.bpl",
            $@"bin\emshosting{ProductVersion}.bpl",
            $@"bin\EMSMultiTenantConsole.exe",
            $@"bin\EMSServer.dll",
            $@"bin\emsserverapi{ProductVersion}.bpl",
            $@"bin\emsserverresource{ProductVersion}.bpl",
            $@"bin\EMUVCLHelper{ProductVersion}.bpl",
            $@"bin\etm.exe",
            $@"bin\exceptiondiag{ProductVersion}.bpl",
            $@"bin\ExpertsCreators{ProductVersion}.bpl",
            $@"bin\ExpertsUI{ProductVersion}.bpl",
            $@"bin\FDAdministrator.exe",
            $@"bin\FDExplorer.exe",
            $@"bin\FDMonitor.exe",
            $@"bin\FireDAC{ProductVersion}.bpl",
            $@"bin\FireDACADOIntegrate{ProductVersion}.bpl",
            $@"bin\FireDACADSDriver{ProductVersion}.bpl",
            $@"bin\FireDACASADriver{ProductVersion}.bpl",
            $@"bin\FireDACCommon{ProductVersion}.bpl",
            $@"bin\FireDACCommonDriver{ProductVersion}.bpl",
            $@"bin\FireDACCommonOdbc{ProductVersion}.bpl",
            $@"bin\FireDACDb2Driver{ProductVersion}.bpl",
            $@"bin\FireDACDBXDriver{ProductVersion}.bpl",
            $@"bin\FireDACDBXIntegrate{ProductVersion}.bpl",
            $@"bin\FireDACDSDriver{ProductVersion}.bpl",
            $@"bin\FireDACIBDriver{ProductVersion}.bpl",
            $@"bin\FireDACInfxDriver{ProductVersion}.bpl",
            $@"bin\FireDACMongoDBDriver{ProductVersion}.bpl",
            $@"bin\FireDACMSAccDriver{ProductVersion}.bpl",
            $@"bin\FireDACMSSQLDriver{ProductVersion}.bpl",
            $@"bin\FireDACMySQLDriver{ProductVersion}.bpl",
            $@"bin\FireDACODBCDriver{ProductVersion}.bpl",
            $@"bin\FireDACOracleDriver{ProductVersion}.bpl",
            $@"bin\FireDACPgDriver{ProductVersion}.bpl",
            $@"bin\FireDACSqliteDriver{ProductVersion}.bpl",
            $@"bin\FireDACTDataDriver{ProductVersion}.bpl",
            $@"bin\FlatBox2DDyn.dll",
            $@"bin\fmx{ProductVersion}.bpl",
            $@"bin\fmxase{ProductVersion}.bpl",
            $@"bin\FMXControlStyleViewer.exe",
            $@"bin\fmxdae{ProductVersion}.bpl",
            $@"bin\fmxdesigner{ProductVersion}.bpl",
            $@"bin\fmxFireDAC{ProductVersion}.bpl",
            $@"bin\FMXMetropolisUIStyleViewer.exe",
            $@"bin\FMXMobileStyleViewer.exe",
            $@"bin\fmxobj{ProductVersion}.bpl",
            $@"bin\FMXRESTComponents{ProductVersion}.bpl",
            $@"bin\fmxstyledesigner{ProductVersion}.bpl",
            $@"bin\FMXStyleViewer.exe",
            $@"bin\FMXWinModernStyleViewer.exe",
            $@"bin\Formatter.exe",
            $@"bin\gdbdebugcore{ProductVersion}.bpl",
            $@"bin\gdbdebugide{ProductVersion}.bpl",
            $@"bin\GenDocCLI.exe",
            $@"bin\GenTLB.exe",
            $@"bin\GetIt{ProductVersion}.bpl",
            $@"bin\GetItCmd.exe",
            $@"bin\getithelper{ProductVersion}.dll",
            $@"bin\GetItide{ProductVersion}.bpl",
            $@"bin\gitide{ProductVersion}.bpl",
            $@"bin\grep.exe",
            $@"bin\GuidedTour{ProductVersion}.bpl",
            $@"bin\hgide{ProductVersion}.bpl",
            $@"bin\historyide{ProductVersion}.bpl",
            $@"bin\htmide{ProductVersion}.bpl",
            $@"bin\htmldlgs{ProductVersion}.bpl",
            $@"bin\htmlhelp1{ProductVersion}.bpl",
            $@"bin\httpsrvr.dll",
            $@"bin\ico2icns.exe",
            $@"bin\ideclang.dll",
            $@"bin\ideclang50.dll",
            $@"bin\idefilefilters{ProductVersion}.bpl",
            $@"bin\IDEiOSPlatform{ProductVersion}.bpl",
            $@"bin\IDELSP{ProductVersion}.bpl",
            $@"bin\idesf.dll",
            $@"bin\IDEvcl{ProductVersion}.bpl",
            $@"bin\IDEWin32Platform{ProductVersion}.bpl",
            $@"bin\IDEWin64Platform{ProductVersion}.bpl",
            $@"bin\ilink32.exe",
            $@"bin\ilink64.exe",
            $@"bin\impdef.exe",
            $@"bin\implib.exe",
            $@"bin\IndexerService.dll",
            $@"bin\IndyCore{ProductVersion}.bpl",
            $@"bin\IndyIPClient{ProductVersion}.bpl",
            $@"bin\IndyIPCommon{ProductVersion}.bpl",
            $@"bin\IndyIPServer{ProductVersion}.bpl",
            $@"bin\IndyProtocols{ProductVersion}.bpl",
            $@"bin\IndySystem{ProductVersion}.bpl",
            $@"bin\inet{ProductVersion}.bpl",
            $@"bin\inetdb{ProductVersion}.bpl",
            $@"bin\inetdbbde{ProductVersion}.bpl",
            $@"bin\inetdbxpress{ProductVersion}.bpl",
            $@"bin\ios32debugide{ProductVersion}.bpl",
            $@"bin\ios64debugide{ProductVersion}.bpl",
            $@"bin\iot{ProductVersion}.bpl",
            $@"bin\iotrest{ProductVersion}.bpl",
            $@"bin\itecore{ProductVersion}.bpl",
            $@"bin\iteide{ProductVersion}.bpl",
            $@"bin\iteidebcb{ProductVersion}.bpl",
            $@"bin\iteidew32{ProductVersion}.bpl",
            $@"bin\libbcceval64.dll",
            $@"bin\libbson-1.0.dll",
            $@"bin\libmongoc-1.0.dll",
            $@"bin\LicenseManager.exe",
            $@"bin\LicenseManager.zh.exe",
            $@"bin\LivePreview{ProductVersion}.bpl",
            $@"bin\lnkdfm{ProductVersion}.dll",
            $@"bin\make.exe",
            $@"bin\midas.dll",
            $@"bin\migrationtool.exe",
            $@"bin\MirrorHub{ProductVersion}.bpl",
            $@"bin\mkexp.exe",
            $@"bin\mlcc{ProductVersion}.bpl",
            $@"bin\MobileWizardExpert{ProductVersion}.bpl",
            $@"bin\ModernTheme{ProductVersion}.bpl",
            $@"bin\mod_emsconsole.dll",
            $@"bin\mod_emsserver.dll",
            $@"bin\MultidevicePreview{ProductVersion}.bpl",
            $@"bin\Office2010rt{ProductVersion}.bpl",
            $@"bin\Office2K{ProductVersion}.bpl",
            $@"bin\officeXPrt{ProductVersion}.bpl",
            $@"bin\optpchwizard{ProductVersion}.bpl",
            $@"bin\paclient.exe",
            $@"bin\paclientcore{ProductVersion}.bpl",
            $@"bin\plugview{ProductVersion}.bpl",
            $@"bin\profiledeployide{ProductVersion}.bpl",
            $@"bin\profilemgride{ProductVersion}.bpl",
            $@"bin\projecttargets{ProductVersion}.bpl",
            $@"bin\projpageide{ProductVersion}.bpl",
            $@"bin\pthreadVC264.dll",
            $@"bin\rc.exe",
            $@"bin\rc{ProductVersion}.bpl",
            $@"bin\RcDLL.dll",
            $@"bin\refactoride{ProductVersion}.bpl",
            $@"bin\reFind.exe",
            $@"bin\RESTBackendComponents{ProductVersion}.bpl",
            $@"bin\RESTComponents{ProductVersion}.bpl",
            $@"bin\RESTDebugger.exe",
            $@"bin\rlink32.dll",
            $@"bin\rmtdbg{ProductVersion}.exe",
            $@"bin\RSConsole.exe",
            $@"bin\rtl{ProductVersion}.bpl",
            $@"bin\rw32core.dll",
            $@"bin\samplevisualizers{ProductVersion}.bpl",
            $@"bin\sanctuarylib.dll",
            $@"bin\scktsrvr.exe",
            $@"bin\Scooter.BeyondCompare.Expert.dll",
            $@"bin\sdkmgride{ProductVersion}.bpl",
            $@"bin\SdkTransform.exe",
            $@"bin\SHFolder.dll",
            $@"bin\soapmidas{ProductVersion}.bpl",
            $@"bin\soaprtl{ProductVersion}.bpl",
            $@"bin\soapserver{ProductVersion}.bpl",
            $@"bin\sqlite3.dll",
            $@"bin\startpageide{ProductVersion}.bpl",
            $@"bin\stdvcl40.dll",
            $@"bin\stride{ProductVersion}.bpl",
            $@"bin\svn{ProductVersion}.bpl",
            $@"bin\svnide{ProductVersion}.bpl",
            $@"bin\svnui{ProductVersion}.bpl",
            $@"bin\tasm32.exe",
            $@"bin\tdstrp32.exe",
            $@"bin\tdump.exe",
            $@"bin\tdump64.exe",
            $@"bin\tethering{ProductVersion}.bpl",
            $@"bin\tgide{ProductVersion}.bpl",
            $@"bin\themeloader{ProductVersion}.bpl",
            $@"bin\tlbview{ProductVersion}.bpl",
            $@"bin\tlib.exe",
            $@"bin\tlib{ProductVersion}.bpl",
            $@"bin\tlib64.exe",
            $@"bin\tlibimp.exe",
            $@"bin\todoide{ProductVersion}.bpl",
            $@"bin\touch.exe",
            $@"bin\TrackingSystem{ProductVersion}.bpl",
            $@"bin\tregsvr.exe",
            $@"bin\typelibimport.dll",
            $@"bin\vcl{ProductVersion}.bpl",
            $@"bin\vclactnband{ProductVersion}.bpl",
            $@"bin\vcldb{ProductVersion}.bpl",
            $@"bin\vcldbx{ProductVersion}.bpl",
            $@"bin\vcldesigner{ProductVersion}.bpl",
            $@"bin\vcldsnap{ProductVersion}.bpl",
            $@"bin\vcledge{ProductVersion}.bpl",
            $@"bin\vclFireDAC{ProductVersion}.bpl",
            $@"bin\vclhie{ProductVersion}.bpl",
            $@"bin\vclide{ProductVersion}.bpl",
            $@"bin\vclie{ProductVersion}.bpl",
            $@"bin\vclimg{ProductVersion}.bpl",
            $@"bin\vclmenudesigner{ProductVersion}.bpl",
            $@"bin\VCLRESTComponents{ProductVersion}.bpl",
            $@"bin\vclshlctrls{ProductVersion}.bpl",
            $@"bin\vclsmp{ProductVersion}.bpl",
            $@"bin\VCLStyleViewer.exe",
            $@"bin\vcltest.dll",
            $@"bin\vcltouch{ProductVersion}.bpl",
            $@"bin\vclwinx{ProductVersion}.bpl",
            $@"bin\vclx{ProductVersion}.bpl",
            $@"bin\VisualizationServiceIDE{ProductVersion}.bpl",
            $@"bin\win32debugide{ProductVersion}.bpl",
            $@"bin\win32debugproide{ProductVersion}.bpl",
            $@"bin\Win32ProxyDownloader.exe",
            $@"bin\win64debugide{ProductVersion}.bpl",
            $@"bin\WSDLImp.exe",
            $@"bin\xmlmapper.exe",
            $@"bin\xmlrtl{ProductVersion}.bpl",
            $@"bin\bds\identity{ProductVersion}.bpl",
            $@"bin\livetile\Embarcadero.Metropolis.LaunchAgentHost.exe",
            $@"bin\livetile\Embarcadero.Metropolis.Launcher.exe",
            $@"bin\livetile\Embarcadero.Metropolis.ServiceHost.exe",
            $@"bin\livetile\Embarcadero.Metropolis.Win32Helpers.dll",
            $@"bin\livetile\Microsoft.Samples.Debugging.CorApi.dll",
            $@"bin\livetile\Microsoft.Samples.Debugging.CorApi.NativeApi.dll",
            $@"bin\livetile\Microsoft.Samples.Debugging.MdbgEngine.dll",
            $@"bin\livetile\Microsoft.Samples.Debugging.Native.dll",
            $@"bin\livetile\Newtonsoft.Json.dll",
            $@"bin\livetile\System.Net.Http.Formatting.dll",
            $@"bin\livetile\System.Web.Http.dll",
            $@"bin\livetile\System.Web.Http.SelfHost.dll",
            $@"bin\plugins\mdlimport\mdlimport.exe",
            $@"bin64\adortl{ProductVersion}.bpl",
            $@"bin64\appanalytics{ProductVersion}.bpl",
            $@"bin64\bcbie{ProductVersion}.bpl",
            $@"bin64\bcboffice2010{ProductVersion}.bpl",
            $@"bin64\bcbsmp{ProductVersion}.bpl",
            $@"bin64\bindcomp{ProductVersion}.bpl",
            $@"bin64\bindcompdbx{ProductVersion}.bpl",
            $@"bin64\bindcompfmx{ProductVersion}.bpl",
            $@"bin64\bindcompvcl{ProductVersion}.bpl",
            $@"bin64\bindcompvclsmp{ProductVersion}.bpl",
            $@"bin64\bindengine{ProductVersion}.bpl",
            $@"bin64\bindgraph{ProductVersion}.bpl",
            $@"bin64\borlndmm.dll",
            $@"bin64\cc64{ProductVersion}.dll",
            $@"bin64\cc64{ProductVersion}mt.dll",
            $@"bin64\CloudService{ProductVersion}.bpl",
            $@"bin64\cquery.exe",
            $@"bin64\CustomIPTransport{ProductVersion}.bpl",
            $@"bin64\DataSnapClient{ProductVersion}.bpl",
            $@"bin64\DataSnapCommon{ProductVersion}.bpl",
            $@"bin64\DataSnapConnectors{ProductVersion}.bpl",
            $@"bin64\DataSnapConnectorsFreePascal{ProductVersion}.bpl",
            $@"bin64\DataSnapFireDAC{ProductVersion}.bpl",
            $@"bin64\DataSnapIndy10ServerTransport{ProductVersion}.bpl",
            $@"bin64\DataSnapNativeClient{ProductVersion}.bpl",
            $@"bin64\DataSnapProviderClient{ProductVersion}.bpl",
            $@"bin64\DataSnapServer{ProductVersion}.bpl",
            $@"bin64\DataSnapServerMidas{ProductVersion}.bpl",
            $@"bin64\dbexpress{ProductVersion}.bpl",
            $@"bin64\dbrtl{ProductVersion}.bpl",
            $@"bin64\dbtest.bpl",
            $@"bin64\Dbxadapter.dll",
            $@"bin64\Dbxasa.dll",
            $@"bin64\Dbxase.dll",
            $@"bin64\dbxcds{ProductVersion}.bpl",
            $@"bin64\DbxClientDriver{ProductVersion}.bpl",
            $@"bin64\DBXCommonDriver{ProductVersion}.bpl",
            $@"bin64\DbxDb2.dll",
            $@"bin64\DBXDb2Driver{ProductVersion}.bpl",
            $@"bin64\Dbxfb.dll",
            $@"bin64\DBXFirebirdDriver{ProductVersion}.bpl",
            $@"bin64\Dbxinf.dll",
            $@"bin64\DBXInformixDriver{ProductVersion}.bpl",
            $@"bin64\Dbxint.dll",
            $@"bin64\DBXInterBaseDriver{ProductVersion}.bpl",
            $@"bin64\Dbxmss.dll",
            $@"bin64\Dbxmss9.dll",
            $@"bin64\DBXMSSQLDriver{ProductVersion}.bpl",
            $@"bin64\Dbxmys.dll",
            $@"bin64\DBXMySqlDriver{ProductVersion}.bpl",
            $@"bin64\DBXOdbcDriver{ProductVersion}.bpl",
            $@"bin64\Dbxora.dll",
            $@"bin64\DBXOracleDriver{ProductVersion}.bpl",
            $@"bin64\DBXSqliteDriver{ProductVersion}.bpl",
            $@"bin64\DBXSybaseASADriver{ProductVersion}.bpl",
            $@"bin64\DBXSybaseASEDriver{ProductVersion}.bpl",
            $@"bin64\dsnap{ProductVersion}.bpl",
            $@"bin64\dsnapcon{ProductVersion}.bpl",
            $@"bin64\dsnapxml{ProductVersion}.bpl",
            $@"bin64\dunitrtl{ProductVersion}.bpl",
            $@"bin64\emsclient{ProductVersion}.bpl",
            $@"bin64\emsclientfiredac{ProductVersion}.bpl",
            $@"bin64\EMSConsole.dll",
            $@"bin64\EMSDevConsole.exe",
            $@"bin64\EMSDevServer.exe",
            $@"bin64\EMSDevServerCommand.exe",
            $@"bin64\emsedge{ProductVersion}.bpl",
            $@"bin64\emshosting{ProductVersion}.bpl",
            $@"bin64\EMSMultiTenantConsole.exe",
            $@"bin64\EMSServer.dll",
            $@"bin64\emsserverapi{ProductVersion}.bpl",
            $@"bin64\emsserverresource{ProductVersion}.bpl",
            $@"bin64\EMUVCLHelper{ProductVersion}.bpl",
            $@"bin64\FireDAC{ProductVersion}.bpl",
            $@"bin64\FireDACADOIntegrate{ProductVersion}.bpl",
            $@"bin64\FireDACADSDriver{ProductVersion}.bpl",
            $@"bin64\FireDACASADriver{ProductVersion}.bpl",
            $@"bin64\FireDACCommon{ProductVersion}.bpl",
            $@"bin64\FireDACCommonDriver{ProductVersion}.bpl",
            $@"bin64\FireDACCommonOdbc{ProductVersion}.bpl",
            $@"bin64\FireDACDb2Driver{ProductVersion}.bpl",
            $@"bin64\FireDACDBXDriver{ProductVersion}.bpl",
            $@"bin64\FireDACDBXIntegrate{ProductVersion}.bpl",
            $@"bin64\FireDACDSDriver{ProductVersion}.bpl",
            $@"bin64\FireDACIBDriver{ProductVersion}.bpl",
            $@"bin64\FireDACInfxDriver{ProductVersion}.bpl",
            $@"bin64\FireDACMongoDBDriver{ProductVersion}.bpl",
            $@"bin64\FireDACMSAccDriver{ProductVersion}.bpl",
            $@"bin64\FireDACMSSQLDriver{ProductVersion}.bpl",
            $@"bin64\FireDACMySQLDriver{ProductVersion}.bpl",
            $@"bin64\FireDACODBCDriver{ProductVersion}.bpl",
            $@"bin64\FireDACOracleDriver{ProductVersion}.bpl",
            $@"bin64\FireDACPgDriver{ProductVersion}.bpl",
            $@"bin64\FireDACSqliteDriver{ProductVersion}.bpl",
            $@"bin64\FireDACTDataDriver{ProductVersion}.bpl",
            $@"bin64\FlatBox2DDyn.dll",
            $@"bin64\fmx{ProductVersion}.bpl",
            $@"bin64\fmxase{ProductVersion}.bpl",
            $@"bin64\fmxdae{ProductVersion}.bpl",
            $@"bin64\fmxFireDAC{ProductVersion}.bpl",
            $@"bin64\fmxobj{ProductVersion}.bpl",
            $@"bin64\FMXRESTComponents{ProductVersion}.bpl",
            $@"bin64\FMXTee9{ProductVersion}.bpl",
            $@"bin64\FMXTeeUI9{ProductVersion}.bpl",
            $@"bin64\ibmonitor{ProductVersion}.bpl",
            $@"bin64\ibxbindings{ProductVersion}.bpl",
            $@"bin64\ibxpress{ProductVersion}.bpl",
            $@"bin64\ideclang50.dll",
            $@"bin64\IndyCore{ProductVersion}.bpl",
            $@"bin64\IndyIPClient{ProductVersion}.bpl",
            $@"bin64\IndyIPCommon{ProductVersion}.bpl",
            $@"bin64\IndyIPServer{ProductVersion}.bpl",
            $@"bin64\IndyProtocols{ProductVersion}.bpl",
            $@"bin64\IndySystem{ProductVersion}.bpl",
            $@"bin64\inet{ProductVersion}.bpl",
            $@"bin64\inetdb{ProductVersion}.bpl",
            $@"bin64\inetdbxpress{ProductVersion}.bpl",
            $@"bin64\Intraweb_140_{ProductVersion}.bpl",
            $@"bin64\iot{ProductVersion}.bpl",
            $@"bin64\iotrest{ProductVersion}.bpl",
            $@"bin64\libbson-1.0.dll",
            $@"bin64\libmongoc-1.0.dll",
            $@"bin64\midas.dll",
            $@"bin64\mod_emsconsole.dll",
            $@"bin64\mod_emsserver.dll",
            $@"bin64\Office2010rt{ProductVersion}.bpl",
            $@"bin64\Office2K{ProductVersion}.bpl",
            $@"bin64\officeXPrt{ProductVersion}.bpl",
            $@"bin64\RESTBackendComponents{ProductVersion}.bpl",
            $@"bin64\RESTComponents{ProductVersion}.bpl",
            $@"bin64\RSConsole.exe",
            $@"bin64\rtl{ProductVersion}.bpl",
            $@"bin64\ScktSrvr.exe",
            $@"bin64\soapmidas{ProductVersion}.bpl",
            $@"bin64\soaprtl{ProductVersion}.bpl",
            $@"bin64\soapserver{ProductVersion}.bpl",
            $@"bin64\Tee9{ProductVersion}.bpl",
            $@"bin64\TeeDB9{ProductVersion}.bpl",
            $@"bin64\TeeUI9{ProductVersion}.bpl",
            $@"bin64\tethering{ProductVersion}.bpl",
            $@"bin64\tlibimp.exe",
            $@"bin64\TRegSvr.exe",
            $@"bin64\vcl{ProductVersion}.bpl",
            $@"bin64\vclactnband{ProductVersion}.bpl",
            $@"bin64\vcldb{ProductVersion}.bpl",
            $@"bin64\vcldsnap{ProductVersion}.bpl",
            $@"bin64\vcledge{ProductVersion}.bpl",
            $@"bin64\vclFireDAC{ProductVersion}.bpl",
            $@"bin64\vclib{ProductVersion}.bpl",
            $@"bin64\vclie{ProductVersion}.bpl",
            $@"bin64\vclimg{ProductVersion}.bpl",
            $@"bin64\VCLRESTComponents{ProductVersion}.bpl",
            $@"bin64\vclshlctrls{ProductVersion}.bpl",
            $@"bin64\vclsmp{ProductVersion}.bpl",
            $@"bin64\vcltest.dll",
            $@"bin64\vcltouch{ProductVersion}.bpl",
            $@"bin64\vclwinx{ProductVersion}.bpl",
            $@"bin64\vclx{ProductVersion}.bpl",
            $@"bin64\xmlrtl{ProductVersion}.bpl",
        };
        }
        public Form1()
        {
            InitializeComponent();
        }

        readonly string DFMHeader = Convert.ToBase64String(new byte[] { 0x54, 0x50, 0x46, 0x30 }); // TPF0


        Dictionary<string, string> Translater = new Dictionary<string, string>();
        void LoadTranslate()
        {
            if (File.Exists("Translate2.xlsx"))
            {
                var workbook = WorkbookFactory.Create("Translate2.xlsx");
                foreach (var worksheet in workbook)
                {
                    foreach (XSSFRow row in worksheet)
                    {
                        if (row.Cells.Count == 1)
                            continue;
                        var cell = row.Cells[0];
                        cell.SetCellType(CellType.String);
                        var y = cell.StringCellValue;
                        cell = row.Cells[1];
                        cell.SetCellType(CellType.String);
                        var f = cell.StringCellValue;
                        if (y != null && f != null)
                        {
                            y = y.Replace("\\r", "\r");
                            y = y.Replace("\\n", "\n");
                            y = y.Replace("\\\\", "\\");
                            f = f.Replace("\\r", "\r");
                            f = f.Replace("\\n", "\n");
                            f = f.Replace("\\\\", "\\");
                            if (!Translater.ContainsKey(y))
                                Translater.Add(y, f);
                        }

                    }
                }
                workbook.Close();
                label1.Text = $"已加载{Translater.Count}个翻译数据";

            }
            else
            {

            }
        }

        void AddtoUntreatedStrings(string value)
        {
            if (!UntreatedStrings.Contains(value))
            {
                UntreatedStrings.Add(value);
            }
        }
        string TranslateDFM(string dfm)
        {
            dfm = DoTranslateDFM(dfm, "Caption");
            dfm = DoTranslateDFM(dfm, "Hint");
            return dfm;
        }
        string DoTranslateDFM(string dfm, string mt)
        {
            var matches = Regex.Matches(dfm, $@"{mt} = '(.*?)'");
            if (matches.Count > 0)
            {
                for (var i = matches.Count - 1; i >= 0; i--) // 倒着替换
                {
                    var match = matches[i].Groups[1];
                    var m = Regex.Match(match.Value, @"&\w");
                    var value = match.Value.Replace("&", "");
                    if (Translater.ContainsKey(value))
                    {
                        value = Translater[value];
                        value = value.Replace("'", "''");
                        if (m.Success)
                        {
                            value += "(" + m.Groups[0].Value.ToUpper() + ")";
                        }
                        var pre = dfm.Substring(0, match.Index);
                        var suffix = dfm.Substring(match.Index + match.Length);
                        dfm = pre + value + suffix;
                    }
                    else
                    {
                        AddtoUntreatedStrings(value);
                    }
                }
            }
            return dfm;
        }



        List<string> GetDFMCaptions(string dfm)
        {
            List<string> result = new List<string>();
            var matches = Regex.Matches(dfm, @"Caption = '(.*?)'");
            if (matches.Count > 0)
            {
                for (var i = matches.Count - 1; i >= 0; i--) // 倒着替换
                {
                    var match = matches[i].Groups[1];
                    var m = match.Value.Replace("&", "");
                    result.Add(m);
                }
            }
            return result;
        }

        string Dfm2Text(byte[] dfm)
        {
            var input = new TMemoryStream();
            input.WriteBuffer(dfm, dfm.Length);
            input.Position = 0;
            using var outputS = new MemoryStream();
            using var output = new TCLRStreamWrapper(outputS);
            Classes.ObjectBinaryToText(input, output);
            input.Free();
            outputS.Seek(0, SeekOrigin.Begin);
            byte[] obytes = new byte[outputS.Length];
            outputS.Read(obytes, 0, obytes.Length);
            return Encoding.Default.GetString(obytes);
        }

        byte[] Text2Dfm(string text)
        {
            using var inputS = new MemoryStream();
            var buf = Encoding.Default.GetBytes(text);
            inputS.Write(buf, 0, buf.Length);
            inputS.Seek(0, SeekOrigin.Begin);
            using var input = new TCLRStreamWrapper(inputS);

            using var outputS = new MemoryStream();
            using var output = new TCLRStreamWrapper(outputS);
            Classes.ObjectTextToBinary(input, output);

            outputS.Seek(0, SeekOrigin.Begin);
            byte[] obytes = new byte[outputS.Length];
            outputS.Read(obytes, 0, obytes.Length);

            return obytes;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            LoadTranslate();
            LoadVersions();
        }


        Dictionary<string, Dictionary<string, object>> Versions = new Dictionary<string, Dictionary<string, object>>();
        void LoadVersions()
        {
            using var registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Embarcadero\BDS");
            if (registryKey != null)
            {
                Versions.Clear();
                var keys = registryKey.GetSubKeyNames();
                foreach (var key in keys)
                {
                    using var sk = registryKey.OpenSubKey(key);
                    var v = new Dictionary<string, object>();
                    foreach (var name in sk.GetValueNames())
                    {
                        v.Add(name, sk.GetValue(name));
                    }
                    Versions.Add(key, v);
                }
                comboBox1.Items.Clear();
                foreach (var v in Versions.Keys)
                {
                    comboBox1.Items.Add(v);
                }
                if (comboBox1.Items.Count > 0)
                    comboBox1.SelectedIndex = 0;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string[] exts = new string[] { ".exe", ".bpl", ".dll" };
            var path = @"D:\Program Files (x86)\Embarcadero\Studio\21.0\";
            var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                var ext = Path.GetExtension(file);
                if (exts.Contains(ext))
                {
                    var value = file.Replace(path, "");
                    Debug.WriteLine($"@\"{value}\",");
                }
            }
        }

        void AddMessage(string msg)
        {
            listBox1.Items.Add(msg);
        }

        List<string> UntreatedStrings = new List<string>();
        private void bw_Translater_DoWork(object sender, DoWorkEventArgs e)
        {
            UntreatedStrings.Clear();
            AddMessage($"开始翻译");
            StartTranslate();
            AddMessage($"翻译完成");

            label3.Text = $"有{UntreatedStrings.Count}个未处理字符串";
        }

        void StartTranslate()
        {
            var rootDir = Versions[comboBox1.Text]["RootDir"] as string;
            var pv = Versions[comboBox1.Text]["ProductVersion"] as string;
            UpdateTargetFiles($"{pv}0");
            foreach (var file in TranslateNativePEFiles)
            {
                var fn = Path.Combine(rootDir, file);
                if (!File.Exists(fn))
                    continue;
                AddMessage($"开始处理 {fn} ");
                if (TranslateNativePE(fn))
                {
                    AddMessage($"处理成功 {fn} ");
                }
                else
                {
                    AddMessage($"处理失败 {fn} ");
                }
            }
            foreach (var file in TranslateDotNetFiles)
            {
                var fn = Path.Combine(rootDir, file);
                if (!File.Exists(fn))
                    continue;
                AddMessage($"开始处理 {fn} ");
                if (TranslateDotNetPE(fn))
                {
                    AddMessage($"处理成功 {fn} ");
                }
                else
                {
                    AddMessage($"处理失败 {fn} ");
                }
            }
        }

        bool TranslateDotNetPE(string filename)
        {
            var targetDir = Path.Combine(Path.GetDirectoryName(filename), "zh");
            if (!Directory.Exists(targetDir))
                Directory.CreateDirectory(targetDir);
            var targetFile = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(filename) + ".resources.dll");
            using var module = ModuleDefMD.Load(filename);
            if (!module.HasResources)
                return false;
            var aChanged = false;
            for (var j = 0; j < module.Resources.Count; j++)
            {
                var r = module.Resources[j];
                if (r.ResourceType != ResourceType.Embedded || r.Name.IndexOf("resources") == -1)
                    continue;
                ResourceElementSet elementSet = ResourceReader.Read(module, (r as EmbeddedResource).CreateReader());
                bool changed = false;
                for (var i = 0; i < elementSet.ResourceElements.Count(); i++)
                {
                    var element = elementSet.ResourceElements.ElementAt(i);
                    if (element.ResourceData is BuiltInResourceData)
                    {
                        var br = element.ResourceData as BuiltInResourceData;
                        if (br.Code == ResourceTypeCode.String)
                        {
                            var value = br.Data as string;
                            var m = Regex.Match(value, @"&\w");
                            value = value.Replace("&", "");
                            if (Translater.ContainsKey(value))
                            {
                                value = Translater[value];
                                if (m.Success)
                                {
                                    value += "(" + m.Groups[0].Value.ToUpper() + ")";
                                }
                                element.ResourceData = new BuiltInResourceData(ResourceTypeCode.String, value);
                                elementSet.Add(element);
                                changed = true;
                            }
                            else
                            {
                                AddtoUntreatedStrings(value);
                            }
                        }
                    }
                }
                if (changed)
                {
                    var ms = new MemoryStream();
                    ResourceWriter.Write(module, ms, elementSet);
                    module.Resources[j] = new EmbeddedResource(r.Name, ms.GetBuffer(), r.Attributes);
                    aChanged = true;
                }
            }
            if (!aChanged)
                return false;
            if (File.Exists(targetFile))
                File.Delete(targetFile);
            //for (var i = module.Types.Count - 1; i > 0; i--)
            //module.Types.RemoveAt(i);
            module.Write(targetFile);
            return true;
        }

        bool TranslateNativePE(string filename)
        {
            var p = filename;
            var image = PEImage.FromFile(p);
            if (image.Resources == null)
                return false;
            var stringResource = StringResource.FromDirectory(image.Resources);
            if (stringResource != null)
            {
                for (var i = 0; i < stringResource.Entries.Count; i++)
                {
                    var sr = stringResource.Entries.Keys.ElementAt(i);
                    var vs = stringResource.Entries[sr];

                    for (var j = 0; j < vs.Entries.Count; j++)
                    {
                        var v = vs.Entries.Keys.ElementAt(j);
                        var value = vs.Entries[v];
                        var m = Regex.Match(value, @"&\w");
                        value = value.Replace("&", "");
                        if (Translater.ContainsKey(value))
                        {
                            value = Translater[value];
                            if (m.Success)
                            {
                                value += "(" + m.Groups[0].Value.ToUpper() + ")";
                            }
                            vs.Entries[v] = value;
                        }
                        else
                        {
                            AddtoUntreatedStrings(value);
                        }
                    }
                    stringResource.Entries[sr] = vs;
                }
                stringResource.WriteToDirectory(image.Resources);
            }

            var rcDataResource = RCDataResource.FromDirectory(image.Resources);
            if (rcDataResource != null)
            {
                foreach (var vv in rcDataResource.Entries.Keys)
                {
                    var data = rcDataResource.Entries[vv].Data;
                    var sig = data.Take(4).ToArray();
                    if (Convert.ToBase64String(sig) == DFMHeader)
                    {
                        var dfmText = Dfm2Text(data);
                        dfmText = TranslateDFM(dfmText);
                        var tempData = Text2Dfm(dfmText);
                        rcDataResource.Entries[vv].SetData(tempData);
                    }
                }

                rcDataResource.WriteToDirectory(image.Resources);
            }

            var targetFile = Path.ChangeExtension(p, ".chs");
            if (File.Exists(targetFile))
                File.Delete(targetFile);
            using (FileStream stream = new FileStream(targetFile, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    new ResourcePEFileBuilder().CreateFile(image).Write(new BinaryStreamWriter(stream));
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        bool LoadFromNativePE(string file1, string file2, ref ISheet workSheet)
        {
            var image1 = PEImage.FromFile(file1);
            var image2 = PEImage.FromFile(file2);
            if (image1.Resources == null || image2.Resources == null)
                return false;
            var stringResource1 = StringResource.FromDirectory(image1.Resources);
            var stringResource2 = StringResource.FromDirectory(image2.Resources);
            if (stringResource1 != null)
            {
                for (var i = 0; i < stringResource1.Entries.Count; i++)
                {
                    var sr = stringResource1.Entries.Keys.ElementAt(i);
                    var vs = stringResource1.Entries[sr];
                    for (var j = 0; j < vs.Entries.Count; j++)
                    {
                        if (j >= 16)
                            break;
                        var v = vs.Entries.Keys.ElementAt(j);
                        var value1 = vs.Entries[v];
                        var value2 = stringResource2.Entries[sr].Entries[v];
                        if (value1 != value2)
                        {
                            var row = workSheet.CreateRow(workSheet.LastRowNum + 1);
                            row.CreateCell(0).SetCellValue(value1);
                            row.CreateCell(1).SetCellValue(value2);
                        }
                    }
                }
            }
            var rcDataResource1 = RCDataResource.FromDirectory(image1.Resources);
            var rcDataResource2 = RCDataResource.FromDirectory(image2.Resources);
            if (rcDataResource1 != null)
            {
                foreach (var vv in rcDataResource1.Entries.Keys)
                {
                    var data = rcDataResource1.Entries[vv].Data;
                    var sig = data.Take(4).ToArray();
                    if (Convert.ToBase64String(sig) == DFMHeader)
                    {
                        var dfmText1 = Dfm2Text(data);
                        var dfmText2 = Dfm2Text(rcDataResource2.Entries[vv].Data);

                        var l1 = GetDFMCaptions(dfmText1);
                        var l2 = GetDFMCaptions(dfmText2);
                        if (l1.Count == l2.Count)
                        {
                            for (var i = 0; i < l1.Count; i++)
                            {
                                var value1 = l1[i];
                                var value2 = l2[i];
                                if (value1 != value2 && value1.Trim() != "" && value2.Trim() != "")
                                {
                                    var row = workSheet.CreateRow(workSheet.LastRowNum + 1);
                                    row.CreateCell(0).SetCellValue(value1);
                                    row.CreateCell(1).SetCellValue(value2);
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }

        private void btn_StartTranslate_Click(object sender, EventArgs e)
        {
            bw_Translater.RunWorkerAsync();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (UntreatedStrings.Count > 0)
            {
                var book = new HSSFWorkbook();
                var sheet = book.CreateSheet();
                int i = 0;
                foreach (var us in UntreatedStrings)
                {
                    var row = sheet.CreateRow(i++);
                    var cell = row.CreateCell(0);
                    cell.SetCellValue(us);
                }
                using var fs = File.OpenWrite("UntreatedStrings.xlsx");
                book.Write(fs);
                Clipboard.SetText(string.Join("\r\n", UntreatedStrings));
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bw_Google.RunWorkerAsync();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            WordTranslator.Cancel();
        }

        private void btn_EN_Click(object sender, EventArgs e)
        {
            SwitchLang("EN");
        }

        private void btn_CHS_Click(object sender, EventArgs e)
        {
            SwitchLang();
        }

        void SwitchLang(string lang = "CHS")
        {
            using var registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Embarcadero\Locales", true);
            if (registryKey != null)
            {
                var names = registryKey.GetValueNames();
                foreach (var name in names)
                {
                    registryKey.SetValue(name, lang);
                }
                registryKey.Close();
            }
        }

        WordTranslator WordTranslator;
        private void bw_Google_DoWork(object sender, DoWorkEventArgs e)
        {
            WordTranslator = new WordTranslator("Word.xlsx");
            WordTranslator.DoTranslte();
        }

        private void btn_ReadTranslate_Click(object sender, EventArgs e)
        {
            bw_Read.RunWorkerAsync();
        }

        private void bw_Read_DoWork(object sender, DoWorkEventArgs e)
        {
            var rootDir = Versions[comboBox1.Text]["RootDir"] as string;

            var workbook = WorkbookFactory.Create(@"Load.xlsx");
            var worksheet = workbook.CreateSheet();
            foreach (var file in TranslateNativePEFiles)
            {
                var fn = Path.Combine(rootDir, file);
                var fnt = Path.ChangeExtension(fn, ".chs");
                if (!File.Exists(fn) || !File.Exists(fnt))
                    continue;
                LoadFromNativePE(fn, fnt, ref worksheet);
            }
            using (var fs = new FileStream(@"Load.xlsx", FileMode.Create))
            {
                workbook.Write(fs);
            }
        }
    }
}
